package core;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

/**
 * @author Arno de Jager
 *
 */
public class Launcher extends JApplet implements ActionListener {

	private ArrayList<Map> maps = new ArrayList<Map>();
	private int difficulty;
	
	private String userName = "";
	private String userEmail = "";
	
	private JButton loginButton;
	
	public void init() {
		setSize( 990, 485 );
		
		chooseDifficulty();
	}
	
	public void chooseMap() {
		generateMaps();
		setContentPane( preView() );
		validate();
	}
	
	/**
	 * Vult de array met maps
	 */
	private void generateMaps() {
		maps.clear();
		
		/* Map 1 */
		ArrayList<ArrayList<Point2D.Double>> routes = new ArrayList<ArrayList<Point2D.Double>>();
		
		// Route 1
		ArrayList<Point2D.Double> route1 = new ArrayList<Point2D.Double>();
		route1.add( new Point2D.Double( -20, 250 ) );
		route1.add( new Point2D.Double( 550, 250 ) );
		routes.add(route1);
		
		maps.add ( new Map( new core.Base( new Point2D.Double( 550, 230) ), routes, 1 ) );
		
		/* Map 2 */
		routes = new ArrayList<ArrayList<Point2D.Double>>();
		
		// Route 1
		ArrayList<Point2D.Double> route2 = new ArrayList<Point2D.Double>();
		route2.add( new Point2D.Double( -10, 100 ) );
		route2.add( new Point2D.Double( 550, 100 ) );
		route2.add( new Point2D.Double( 550, 216 ) );
		route2.add( new Point2D.Double( 100, 216 ) );
		routes.add(route2);
		
		// Route 2
		ArrayList<Point2D.Double> route3 = new ArrayList<Point2D.Double>();
		route3.add( new Point2D.Double( -10, 360 ) );
		route3.add( new Point2D.Double( 550, 360 ) );
		route3.add( new Point2D.Double( 550, 265 ) );
		route3.add( new Point2D.Double( 100, 265 ) );
		routes.add(route3);
		
		maps.add ( new Map( new Base( new Point2D.Double( 50, 215) ), routes, 2 ) );
		
		/* Map 3 */
		routes = new ArrayList<ArrayList<Point2D.Double>>();
		
		// Route 1
		ArrayList<Point2D.Double> route4 = new ArrayList<Point2D.Double>();
		route4.add( new Point2D.Double( 700, 450 ) );
		route4.add( new Point2D.Double( 700, 320 ) );
		route4.add( new Point2D.Double( 90, 320 ) );
		route4.add( new Point2D.Double( 90, 200 ) );
		route4.add( new Point2D.Double( 670, 200 ) );
		route4.add( new Point2D.Double( 670, 95 ) );
		route4.add( new Point2D.Double( 185, 95 ) );
		routes.add(route4);
		
		maps.add ( new Map( new Base( new Point2D.Double( 120, 75) ), routes, 3 ) );
		
		/* Map 4 */
		routes = new ArrayList<ArrayList<Point2D.Double>>();
		
//		 Route 1
		ArrayList<Point2D.Double> route5 = new ArrayList<Point2D.Double>();
		route5.add( new Point2D.Double( 710, 450 ) );
		route5.add( new Point2D.Double( 710, 15 ) );
		route5.add( new Point2D.Double( 165, 15 ) );
		//route5.add( new Point2D.Double( 165, 35 ) );
		route5.add( new Point2D.Double( 165, 320 ) );
		//route5.add( new Point2D.Double( 240, 320 ) );
		route5.add( new Point2D.Double( 370, 320 ) );
		route5.add( new Point2D.Double( 370, 240 ) );
		routes.add( route5 );
		
		// Route 2
		ArrayList<Point2D.Double> route6 = new ArrayList<Point2D.Double>();
		route6.add( new Point2D.Double( 60, -40 ) );
		route6.add( new Point2D.Double( 60, 380 ) );
		//route6.add( new Point2D.Double( 60, 340 ) );
		//route6.add( new Point2D.Double( 90, 380 ) );
		route6.add( new Point2D.Double( 555, 380 ) );
		route6.add( new Point2D.Double( 555, 90 ) );
		//route6.add( new Point2D.Double( 475, 90 ) );
		route6.add( new Point2D.Double( 370, 90 ) );
		route6.add( new Point2D.Double( 370, 190 ) );
		routes.add( route6 );
		
		maps.add ( new Map( new Base( new Point2D.Double( 330, 200) ), routes, 4 ) );
		
		/* Map 5 */
		routes = new ArrayList<ArrayList<Point2D.Double>>();
		
		// Route 1
		ArrayList<Point2D.Double> route7 = new ArrayList<Point2D.Double>();
		route7.add( new Point2D.Double( 55, -40 ) );
		route7.add( new Point2D.Double( 55, 380 ) );
		route7.add( new Point2D.Double( 375, 380 ) );
		route7.add( new Point2D.Double( 375, 70 ) );
		route7.add( new Point2D.Double( 610, 70 ) );
		route7.add( new Point2D.Double( 610, 290 ) );
		routes.add( route7 );
		
		maps.add ( new Map( new Base( new Point2D.Double( 565, 285) ), routes, 5 ) );
	}
	
	/**
	 * @param i nummer van de map (Geen index)
	 */
	private void setMap(int i) {
		i--;
		Model m = new Model( this, maps.get(i), difficulty, false );
		
		Controller c = new Controller( m );
		
		setContentPane( c.getView() );
		
		//Eventueel nog inlog gegevens doorsturen
		if( !userName.equals("") && !userEmail.equals("") ) {
			m.setUser(userName, userEmail);
		}
	}
	
	
	public void chooseDifficulty() {
		setContentPane( difficultyPanel() );
		
		validate();
	}
	
	/**
	 * Maakt een panel om de moeilijkheid te kiezen
	 * @return panel om de moeilijkheid te kiezen
	 */
	private JPanel difficultyPanel()
	{
		JPanel p = new JPanel( new BorderLayout() );
		p.setBackground(new Color(79, 128, 39));
		
		// Bovenaan groet
		JPanel topPanel = new JPanel();
		topPanel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		topPanel.setBackground(new Color(79, 128, 39));
		topPanel.setBorder( new EmptyBorder( 15, 15, 15, 15));
		
		JLabel text = new JLabel("Welcome to Towel Defense 2!");
		text.setForeground( Color.white );
		text.setFont( new Font("Tahoma", Font.BOLD, 18) );
		topPanel.add(text);
		
		p.add( topPanel, BorderLayout.NORTH );
		
		// Nu de 3 moeilijkheidsgraden
		JPanel buttons = new JPanel( new GridLayout(1,3) );		
		buttons.setBackground(new Color(79, 128, 39));
		buttons.add( new Button("Easy", "Difficulty: 1", this) );
		buttons.add( new Button("Normal", "Difficulty: 2", this) );
		buttons.add( new Button("Hard", "Difficulty: 3", this) );
		p.add( buttons, BorderLayout.CENTER );
		
		// Login button
		JPanel bottomPanel = new JPanel( new BorderLayout() );
		bottomPanel.setBorder( new LineBorder(Color.white) );
		bottomPanel.setBackground(new Color(79, 128, 39));
		bottomPanel.setBorder( new EmptyBorder( 15, 15, 15, 15));
		loginButton = new Button("Click here to login", "Login", this);
		bottomPanel.add( loginButton, BorderLayout.EAST );
		
		p.add( bottomPanel, BorderLayout.SOUTH );
		return p;
	}
	
	class Button extends JButton
	{
		Button( String label, String actionCommand, ActionListener l )
		{
			this.setText(label);
			this.addActionListener(l);
			this.setActionCommand(actionCommand);
			this.setBackground( new Color(79, 128, 39) );
			this.setBorder( new EmptyBorder(15, 15, 15, 15) );
			this.setForeground( Color.white );
			this.setFont( new Font("Verdana", Font.BOLD, 12) );
			this.setContentAreaFilled(false);
			this.setBackground(new Color(79, 128, 39));
		}
	}
	
	/**
	 * @return Het paneel met de knoppen van de mappen die je kan kiezen
	 */
	private JPanel preView() {
		//Bovenste paneel
		JPanel top = new JPanel( new BorderLayout() );
		top.setBackground(new Color(79, 128, 39));
		
		//De melding dat je een map moet kiezen
		JPanel topPanel = new JPanel();
		topPanel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		topPanel.setBackground(new Color(79, 128, 39));
		topPanel.setBorder( new EmptyBorder( 15, 15, 15, 15));
		
		JLabel text = new JLabel("Please choose a map");
		text.setForeground( Color.white );
		text.setFont( new Font("Tahoma", Font.BOLD, 18) );
		topPanel.add(text);
		top.add( topPanel, BorderLayout.NORTH );
		
		//Map paneel
		JPanel panel = new JPanel( new GridLayout(2,3) );
		
		//De mappen toevoegen
		for(int i = 1; i <= maps.size(); i++) {
			JButton j = new JButton(new ImageIcon(this.getClass().getResource("../images/map" + i + ".gif")));
			j.addActionListener(this);
			j.setActionCommand("" + i);
			j.setBackground( new Color(79, 128, 39) );
			j.setBorder( new EmptyBorder(5, 5, 5, 5) );
			panel.add(j);
		}
		
		JPanel buttonPanel = new JPanel( new GridLayout(1,3) );		
		buttonPanel.setBackground(new Color(79, 128, 39));
		buttonPanel.add( new Button("Map Editor", "mapeditor", this) );
		panel.add(buttonPanel);
		
		top.add(panel);
		
		return top;
	}
	
	/**
	 * De Actionlistener voor dit gedeelte
	 */
	public void actionPerformed(ActionEvent evt) {
		String command = evt.getActionCommand();
		if( command.equals("Login") ) {
			String userName = JOptionPane.showInputDialog("What is your name?");
			String userEmail = null;
			if( userName != null ) {
				userEmail = JOptionPane.showInputDialog("What is your email?");
			}

			if( userName != null && userEmail != null ) {
				this.userName = userName;
				this.userEmail = userEmail;
				
				loginButton.setEnabled(false);
			}
		}
		else if ( command.startsWith("Difficulty: ") ) {
			String difficulty = command.substring("Difficulty: ".length());
			
			this.difficulty = Integer.parseInt( difficulty );
			
			//Als een moeilijkheidsgraad is gekozen, een map kiezen
			chooseMap();
		}
		else if ( command.equals("mapeditor"))
		{
			Model m = new Model( this, new Map(null, new ArrayList<ArrayList<Point2D.Double>>(), 0), difficulty, true );
			Controller c = new Controller( m );
			setContentPane( c.getView() );
			validate();
		}
		else {
			setMap( Integer.parseInt( command ) );
		}
	}
	
}
